package com.house.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.core.page.TableDataInfo;
import com.house.system.service.IHouseService;
import com.house.system.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.Comment;
import com.house.system.service.ICommentService;

/**
 * 评论Controller
 *
 * @author tc
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/domain/comment")
public class CommentController extends BaseController {
    @Autowired
    private ICommentService commentService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHouseService houseService;


    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('domain:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment) {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
        AjaxResult ajax = AjaxResult.success();
        Comment comment = commentService.selectCommentByCommentId(commentId);
        House house = houseService.selectHouseByHouseId(comment.getHouseId());
        User user = userService.selectUserByUserId(comment.getUserId());
        ajax.put("comment", comment);
        ajax.put("user", user);
        ajax.put("house", house);
        return ajax;
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasPermi('domain:comment:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment) {
        comment.setUserId(getUserId());
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasPermi('domain:comment:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment) {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasPermi('domain:comment:remove')")
    @DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds) {
        return toAjax(commentService.deleteCommentByCommentIds(commentIds));
    }
}
